package com.vnaloha.common.controller;

import java.io.IOException;
import java.io.OutputStream;
import java.sql.Blob;
import java.sql.SQLException;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.IOUtils;
import org.hibernate.Hibernate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.vnaloha.common.model.Document;
import com.vnaloha.common.service.DocumentService;

@Controller
@RequestMapping("/document")
public class DocumentController {
	
	private Logger logger = LoggerFactory.getLogger(DocumentController.class);
	
	@Autowired
	private DocumentService documentService;
	
	@RequestMapping(method = RequestMethod.GET)
	public String index(ModelMap modelMap) {
		
		try {
			modelMap.addAttribute("document", new Document());
			modelMap.addAttribute("documents", documentService.list());
		} catch (Exception e) {
			logger.error("Error when get index of document " + e, e);
		}
		return "documents";
	}
	
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public String add(@ModelAttribute("document") Document document, @RequestParam("file") MultipartFile file) {
		try {
			@SuppressWarnings("deprecation")
			Blob blob = Hibernate.createBlob(file.getInputStream());
			
			document.setFileName(file.getOriginalFilename());
			document.setContent(blob);
			document.setContentType(file.getContentType());
			
		} catch (IOException e) {
			logger.error("Error when add document " + e, e);
		}
		
		try {
			documentService.save(document);
		} catch (Exception e) {
			logger.error("Error when create document " + e, e);
		}
		return "redirect:/document";
		
	}
	
	@RequestMapping("/download/{documentId}")
	public String download(@PathVariable("documentId") Integer documentId, HttpServletResponse response) {
		
		Document doc = documentService.get(documentId);
		try {
			response.setHeader("Content-Disposition", "inline;filename=\"" +doc.getFileName()+ "\"");
            OutputStream out = response.getOutputStream();
            response.setContentType(doc.getContentType());
            IOUtils.copy(doc.getContent().getBinaryStream(), out);
            out.flush();
            out.close();
		} catch (IOException e) {
			logger.debug("Error when load file " + e, e);
		} catch (SQLException e) {
			logger.debug("Error sql file " + e, e);
		}
		
		return null;
	}
	
	@RequestMapping(value = "/delete/{documentId}")
	public String delete(@PathVariable("documentId") Integer documentId) {
		
		documentService.delete(documentId);
		
		return "redirect:/document";
	}

}
